define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'area/index',
                    add_url: 'area/add',
                    edit_url: 'area/edit',
                    del_url: 'area/del',
                    multi_url: 'area/multi',
                    table: 'table',
                },
                commonSearch: false,
                search: false,
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', operate:false,title: __('Id')},
                        {field: 'atitle', title: __('atitle')},
                        {field: 'cityid', title: __('cityid')},
                        {field: 'citydid', title: __('citydid')},
                        {field: 'createtime', title: __('createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updtime', title: __('updtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}


                    ]
                ]
            });



            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('post-body.bs.table',function () {

              //  $(".btn-editone").data("title", "修改标签"); //自定义弹窗标题
               // $(".btn-editone").data("area", ["80%","80%"]);//修改弹窗大小
            });


        },

        add: function () {
            Controller.api.bindevent();
            //重构
            $("#city").change(function () {
                $("#shiid").css("display",'block');
                var sel = $(this).val();
                $("#h_areadid").empty();
                $.ajax({
                    type: "GET",
                    url: "/api/task/getAreaDown",
                    data: {arreaid:sel},
                    dataType: "json",
                    success: function(data){
                        var len = data.length;
                        var html = "";
                        if(len > 0){
                            for(var i=0;i<len;i++){
                                html += ' <option pid="'+ data[i]['parentId'] +'" value="'+ data[i]['areaId'] +'">'+ data[i]['areaName'] +'</option>';
                            }
                        }
                        $("#h_areadid").append(html);
                    }
                });

            })


        },
        edit: function () {
            Controller.api.bindevent();
            $(".btn-refresh").trigger('click');
            //parent.location.reload();//刷新这个页面

            //选中城市
            var sel = $("#city").attr("ids");
            $("#city option[value="+ sel +"]").prop("selected",true);

            //获取对应省份市区
            var sel2 = $("#h_areadid").attr("ids");
            $.ajax({
                type: "GET",
                url: "/api/task/getAreaDown",
                data: {arreaid:sel},
                dataType: "json",
                success: function(data){
                    var len = data.length;
                    var html = "";
                    if(len > 0){
                        for(var i=0;i<len;i++){
                            html += ' <option pid="'+ data[i]['parentId'] +'" value="'+ data[i]['areaId'] +'">'+ data[i]['areaName'] +'</option>';
                        }
                    }
                    $("#h_areadid").append(html);
                    $("#h_areadid option[value="+ sel2 +"]").prop("selected",true);
                }
            });

            //重构
            $("#city").change(function () {
                var sel = $(this).val();
                $("#h_areadid").empty();
                $.ajax({
                    type: "GET",
                    url: "/api/task/getAreaDown",
                    data: {arreaid:sel},
                    dataType: "json",
                    success: function(data){
                        var len = data.length;
                        var html = "";
                        if(len > 0){
                            for(var i=0;i<len;i++){
                                html += ' <option pid="'+ data[i]['parentId'] +'" value="'+ data[i]['areaId'] +'">'+ data[i]['areaName'] +'</option>';
                            }
                        }
                        $("#h_areadid").append(html);
                    }
                });

            })

        },
        retprice: function () {//没新增一个弹窗页面 这里都要配置，否则 执行成功后 无法关闭弹窗
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

            }
        }
    };




    //申请退款
    $("#applyMoney").click(function () {
        var Oids = $("#c-orderIds").val();
        layer.confirm('是否确定申请退款?', {icon: 3, title:'申请退款'}, function(index){

            $.ajax({
                type: "POST",
                url: "order/applyMoney",
                data: {Oids:Oids},
                dataType: "json",
                success: function(data){
                    if(data.code == 1){
                        layer.msg(data.msg,{time:2000,icon: 6})
                        $("#applyMoney").attr("disabled","disabled");
                    }else{
                        layer.msg(data.msg,{time:2000,icon: 5})
                        return false;
                    }

                }
            });

            layer.close(index);
        });

    })


    //执行退款
    $("#doReturnMoney").click(function () {
        var Oids = $("#c-orderIds").val();
        layer.confirm('是否确定执行退款，操作不可回退?', {icon: 3, title:'执行退款'}, function(index){

            $.ajax({
                type: "POST",
                url: "order/doreturnprice",
                data: {Oids:Oids},
                dataType: "json",
                success: function(data){
                    if(data.code == 1){
                        layer.msg(data.msg,{time:2000,icon: 6})
                        $("#doReturnMoney").attr("disabled","disabled");
                    }else{
                        layer.msg(data.msg,{time:2000,icon: 5})
                        return false;
                    }

                }
            });

            layer.close(index);
        });

    })

    return Controller;
});
